package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
  @Autowired
  private SetmealMapper setmealMapper;
  @Autowired
  private SetmealDishMapper setmealDishMapper;
  @Autowired
  private DishMapper dishMapper;
  
  @Override
  public PageResult page(SetmealPageQueryDTO queryDTO) {
    PageHelper.startPage(queryDTO.getPage(), queryDTO.getPageSize());
    Page<SetmealVO> page = setmealMapper.page(queryDTO);
    return new PageResult(page.getTotal(), page.getResult());
  }
  
  @Override
  public void add(SetmealDTO setmealDTO) {
    Setmeal setmeal = new Setmeal();
    BeanUtils.copyProperties(setmealDTO, setmeal);
    // 插入套餐基本数据
    setmealMapper.insert(setmeal);
    
    // 获取套餐菜肴
    List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
    
    if(!setmealDishes.isEmpty()) {
      // 设置套餐id
      setmealDishes.forEach(s -> s.setSetmealId(setmeal.getId()));
      setmealDishMapper.batchInsert(setmealDishes);
    }
  }
  
  @Override
  public void update(SetmealDTO setmealDTO) {
    Setmeal setmeal = new Setmeal();
    BeanUtils.copyProperties(setmealDTO, setmeal);
    // 1.先修改套餐基本信息
    setmealMapper.update(setmeal);
    
    // 2.更新套餐菜品信息
    // 2.1先批量删除所有相关数据
    List<Long> ids = Collections.singletonList(setmealDTO.getId());
    setmealDishMapper.batchDelete(ids);
    // 2.2再批量添加
    List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
    setmealDishes.forEach(s -> s.setSetmealId(setmeal.getId())); // 设置套餐id
    setmealDishMapper.batchInsert(setmealDishes);
  }
  
  /**
   * 批量删除套餐
   * @param ids
   */
  @Override
  public void batchDel(List<Long> ids) {
    // 1.判断套餐状态是否售卖中
    for (Long id : ids) {
      SetmealVO setmealVO = setmealMapper.findById(id);
      if(setmealVO.getStatus() == 1) {
        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
      }
    }
    // 2.判断套餐是否被关联
    List<Long> dishIds =  setmealDishMapper.getDishIdsBySetmealIds(ids);
    if(dishIds != null && !dishIds.isEmpty()) {
      throw new DeletionNotAllowedException("当前套餐关联了菜品,不能删除");
    }
    
    // 3.删除套餐
    setmealMapper.batchDel(ids);
    // 4.删除套餐关联的菜品
    setmealDishMapper.batchDelete(ids);
  
  }
  
  /**
   *
   * @param status
   * @param id
   */
  @Override
  public void changeStatus(Integer status, Long id) {
    // 套餐内包含未启售菜品，无法启售
    List<Long> dishIds = setmealDishMapper.getDishIdsBySetmealIds(Arrays.asList(id));
    for (Long dishId : dishIds) {
      DishVO dishVO = dishMapper.findById(dishId);
      if(dishVO == null) throw new BaseException(MessageConstant.ID_NOT_FOUND);
      if(dishVO.getStatus() == 0) {
        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
      }
    }
    
    Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
    setmealMapper.update(setmeal);
  }
  
  /**
   * 查询套餐列表
   * @param setmeal
   */
  @Override
  public List<Setmeal> list(Setmeal setmeal) {
    List<Setmeal> list = setmealMapper.list(setmeal);
    return list;
  }
  
  /**
   * 根据套餐ID获取菜品项
   * @param setmealId
   */
  @Override
  public List<DishItemVO> getDishItemById(Long setmealId) {
    return setmealMapper.getDishItemBySetmealId(setmealId);
  }
  
  /**
   * 根据套餐id查询套餐和对应菜品列表
   * @param id
   */
  @Override
  public SetmealVO findById(Long id) {
    // 方式1：分两次查询，先查出套餐基本信息
    // SetmealVO setmealVO = setmealMapper.findById(id);
    // if(setmealVO != null) {
    //   // 再查下套餐对应的菜品
    //   List<SetmealDish> dishes = setmealDishMapper.findBySetmealId(id);
    //   setmealVO.setSetmealDishes(dishes);
    // }
    //
    // return setmealVO;
    
    // 方式2：使用ResultMap, 显式定义所有映射
    return setmealMapper.findSetmealWithDishesById(id);
  }
}
